class Solution {
    private int ans = 0, cnt = 0;

    public int kthLargest(TreeNode root, int k) {
        dfs(root, k);
        return ans;
    }

    private void dfs(TreeNode root, int k) {
        if (root == null)
            return;
        dfs(root.right, k);
        if (++cnt == k) {
            ans = root.val;
        }
        dfs(root.left, k);
    }
}